package com.xie.smfs;

import com.xie.smfs.command.OpenGhostScreenCommand;
import com.xie.smfs.common.events.PlayerEvents;
import com.xie.smfs.entity.*;
import com.xie.smfs.entity.GhostEntity;
import com.xie.smfs.event.GhostInteractionHandler;
import com.xie.smfs.event.screen.GhostControlScreenHandler;
import com.xie.smfs.registry.*;
import com.xie.smfs.handler.SecondInventoryScreenHandler;
import com.xie.smfs.event.ModEvents;
import com.xie.smfs.handler.SecondInventoryScreenHandler;
import com.xie.smfs.event.ModLootTableEvents;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.fabricmc.fabric.api.event.player.UseEntityCallback; // 使用新的 API
import net.fabricmc.fabric.api.event.player.UseEntityCallback;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType;
import net.minecraft.entity.SpawnGroup;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.tag.BiomeTags;

import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;
import net.minecraft.server.MinecraftServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.xie.smfs.event.DoorInteractionHandler;
import com.xie.smfs.event.PlayerJoinListener;
import com.xie.smfs.util.PlayerBlockActionTracker;

public class Smfs implements ModInitializer {

    public static final String MOD_ID = "smfs";
    public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
    public static final Identifier SECOND_INVENTORY_ID = new Identifier(MOD_ID, "second_inventory");
    // 添加 OPEN_GHOST_SCREEN_PACKET 常量
    public static final Identifier OPEN_GHOST_SCREEN_PACKET = new Identifier(MOD_ID, "open_ghost_screen");

    public static ScreenHandlerType<SecondInventoryScreenHandler> SECOND_INVENTORY_SCREEN_HANDLER;

    @Override
public void onInitialize() {
    PlayerBlockActionTracker.registerEvents();
        //注册鬼属性
        FabricDefaultAttributeRegistry.register(ModEntities.FOOD_GHOST, GhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.TAITOU_GHOST, GhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.DITOU_GHOST, GhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.QIAOMEN_GHOST, GhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.GHOST_MERCHANT, GhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.BOX_GHOST, BoxGhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.VILLAGER_GHOST, VillagerGhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.JUMP_GHOST, JumpGhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.FOG_GHOST, FogGhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.UNTOUCHABLE_GHOST, UntouchableGhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.DEATH_SIGHT_GHOST, DeathSightGhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.BLOCK_GHOST, BlockGhostEntity.createGhostAttributes());
        FabricDefaultAttributeRegistry.register(ModEntities.LOST_GHOST, LostGhostEntity.createGhostAttributes());

        //注册buff
        ModEffects.registerEffects();
        //注册灵异属性
        com.xie.smfs.effect.SpiritAttributes.register();
        //注册方块
        ModBlocks.registerBlocks();
        //注册方块实体 - 添加这行！
        ModBlockEntities.registerBlockEntities();
        //注册物品
        ModItems.registerItems();
        //注册战利品表
        ModLootTableEvents.register();
        //注册事件
        ModSounds.registerSounds();
        ModEvents.registerEvents();
        PlayerEvents.register();
        //注册门交互处理器
        DoorInteractionHandler.register();
        //注册实体攻击监听器
        com.xie.smfs.event.EntityAttackListener.register();
        //注册玩家加入监听器
        PlayerJoinListener.register();

        ServerLifecycleEvents.SERVER_STARTED.register(this::onServerStarted);
        UseEntityCallback.EVENT.register(new GhostInteractionHandler());
        // 注册驭鬼界面处理器
        ModScreenHandlers.registerScreenHandlers();
        // 注册指令
        CommandRegistrationCallback.EVENT.register(OpenGhostScreenCommand::register);

        // 在 onInitialize() 方法中添加数据包处理器
        ServerPlayNetworking.registerGlobalReceiver(OPEN_GHOST_SCREEN_PACKET,
                (server, player, handler, buf, responseSender) -> {
                    server.execute(() -> {
                        try {
                            player.openHandledScreen(new GhostControlScreenHandler.GhostControlFactory());
                            Smfs.LOGGER.info("玩家 {} 通过按钮打开了厉鬼控制界面", player.getName().getString());
                        } catch (Exception e) {
                            Smfs.LOGGER.error("打开厉鬼控制界面失败", e);
                            player.sendMessage(Text.literal("§c打开界面失败: " + e.getMessage()), true);
                        }
                    });
                });
        // 注册屏幕处理器
        SECOND_INVENTORY_SCREEN_HANDLER = Registry.register(
                Registries.SCREEN_HANDLER,
                SECOND_INVENTORY_ID,
                new ExtendedScreenHandlerType<>(SecondInventoryScreenHandler::createFromPacket)
        );

        LOGGER.info("Screen handler registered");
        LOGGER.info("Second Inventory mod initialized - Press B to open second inventory");
    }

    // 工厂类用于创建屏幕处理器
    public static class Factory implements NamedScreenHandlerFactory {
        @Override
        public net.minecraft.screen.ScreenHandler createMenu(int syncId, net.minecraft.entity.player.PlayerInventory inventory, net.minecraft.entity.player.PlayerEntity player) {
            return new SecondInventoryScreenHandler(syncId, inventory);
        }

        @Override
        public Text getDisplayName() {
            return Text.translatable("container.second_inventory");
        }
    }

    private void onServerStarted(MinecraftServer server) {
        // 服务器启动时的逻辑
    }
}